﻿// yaml-cpp dll.h
#define YAML_CPP_STATIC_DEFINE

// CMakeProject-test.cpp: 定义应用程序的入口点。
//
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

#include <yaml-cpp/yaml.h>

#include "CMakeProject-test.h"
#include "netserver.h"
// #include "test.h"
// #include "demo.h"
// #include "https-new-service.h"
#include "http-client.h"
#include "https-service.h"
#include "utils.h"

using namespace std;
using namespace nets;

// 加载配置
YAML::Node load_yaml();

int initialize_winsock() {
    WSADATA wsaData;
    int     iResult;
    // 初始化 Winsock 库
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        cout << "WSAStartup failed: " << iResult << endl;
        return -1;
    }
    return 0;
}

int main() {
    cout << __cplusplus << endl;

	// 设置控制台输出为 UTF-8 编码
	//system("chcp 65001>nul");

    initialize_winsock();

    // 1. socket
     //socket_server();

    // 2. https
    /*DemoService dService;
    dService.test();*/

    // 3. https new service
    // test(0, 0);
    /*server_conf *conf(new server_conf{
        INADDR_ANY,
        443,
        1,
        "cert/server/server.crt",
        "cert/server/server.key",
    });

    HttpsService https;
    https.run(conf);*/

    // 4. http client
    HttpClient client;
    client.test();

    // 5. getaddrinfo
    // my_util_nets::host2ip("www.baidu.com", "80");

    return 0;
}

YAML::Node load_yaml() {
    filesystem::path p("conf\\app.yaml");
    cout << "File Exists: " << filesystem::exists(p) << endl;

    std::ifstream     file(p);
    std::string       line;
    std::stringstream conf_content;
    while (getline(file, line)) {
        conf_content << line << endl;
    }
    file.close();
    cout << conf_content.str() << endl;

    try {
        YAML::Node  _CONFIG = YAML::Load(conf_content.str());
        const auto &server  = _CONFIG["server"];

        return server;
    } catch (runtime_error e) {
        cout << "Parse Failed: " << e.what() << endl;
    }

    YAML::Node nul;
    return nul;
}
